package com.xingdata.jjxc.utils.database;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * 
 * @描述 TODO(数据库脚本变更工具类)
 * @类名称 UpdataDBServer.java
 * @包名 com.newtouch.crm1_2.utility.database
 * @创建时间 2014-10-17 下午1:30:30
 * @创建人 JiangMin
 * @版本号 V1.0
 */
public class UpdateDBTools {
	/**
	 * 获取数据库升级信息
	 * @return
	 */
	@SuppressWarnings("unused")
	public static ArrayList<DatabaseVersion> getUpdateSQL(Context context){
		ArrayList<DatabaseVersion> databaseVersions =null;
		JSONArray databaseVersionJsonArray=null;
		DatabaseVersion databaseVersion = null;
		 ArrayList<String> sqlList=null;
		String strSQL=StringUtil.getAssetsFileToString(context, SystemInfo.UpdateDBSQL);
		try {
			if (StringUtil.isStringNull(strSQL)) {
				JSONObject jsonObject = new JSONObject(strSQL);
				 databaseVersionJsonArray =jsonObject.optJSONArray("databaseVersions");
				 for (int i = 0; i < databaseVersionJsonArray.length(); i++) {
					 databaseVersions= new ArrayList<DatabaseVersion>();
					 JSONObject databaseVersionObject =databaseVersionJsonArray.getJSONObject(i);
					 databaseVersion = new DatabaseVersion();
					 databaseVersion.setOldVersion(Integer.parseInt(databaseVersionObject.optString("OldVersion")));
					 databaseVersion.setNewVersion(Integer.parseInt(databaseVersionObject.optString("NewVersion")));
					 JSONArray sqlJsonArray =databaseVersionObject.optJSONArray("ListSQL");
					 if (sqlList == null) {
							sqlList = new ArrayList<String>();
						} else {
							sqlList.clear();
						}
					 for (int j = 0; j < sqlJsonArray.length(); j++) {
						 sqlList.add(sqlJsonArray.getJSONObject(j).optString("strSQL"));
					}
					 databaseVersion.setListSQL(sqlList);
					 databaseVersions.add(databaseVersion);
				}
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return databaseVersions;
		
	}
	
	
}
